package com.byx.chatclient.model;

import android.widget.Toast;

import com.byx.chatclient.contract.user.ForgetPwdTaskContract;
import com.byx.chatclient.contract.user.RegisterTaskContract;
import com.byx.chatclient.entity.RegisterWay;
import com.byx.chatclient.entity.Result;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ApiUrl;
import com.byx.chatclient.view.activity.user.RegisterActivity;
import com.google.gson.JsonElement;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * ForgetPwdModel
 *
 * @author Superb
 * @date 2021/3/24 21:54
 */
public class ForgetPwdModel implements ForgetPwdTaskContract.Model {
    /**
     * 获取验证码
     *
     * @param number
     * @param callBack
     */
    @Override
    public void getVerificationCode(String number, SimpleCallBack callBack) {
        RetrofitFactory.getBean(ApiUrl.class).getVerificationCodeForChangePwd(number, RegisterWay.BY_EMAIL)
                .enqueue(new Callback<Result<String>>() {
                    @Override
                    public void onResponse(Call<Result<String>> call, Response<Result<String>> response) {
                        Result<String> result;
                        if (response.isSuccessful() && (result = response.body()) != null){
                            String email = result.getData();
                            if (email != null){
                                callBack.onSuccess(email);
                            }else {
                                callBack.onError("验证码发送失败");
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<Result<String>> call, Throwable t) {
                        callBack.onError(t.getMessage());
                    }
                });
    }


    /**
     * 验证邮箱与验证码是否对应
     *
     * @param email
     * @param code
     */
    @Override
    public void checkVerificationCode(String email, String code,SimpleCallBack callBack) {
        RetrofitFactory.getBean(ApiUrl.class).checkVerificationCode(email,code)
                .enqueue(new Callback<Result<Boolean>>() {
                    @Override
                    public void onResponse(Call<Result<Boolean>> call, Response<Result<Boolean>> response) {
                        Result<Boolean> result;
                        if (response.isSuccessful() && (result = response.body()) != null){
                            boolean codeIsRight = result.getData();
                            if (codeIsRight){
                                callBack.onSuccess(null);
                            }else {
                                callBack.onError("验证码错误！");
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<Result<Boolean>> call, Throwable t) {
                        callBack.onError(t.getMessage());
                    }
                });
    }

    /**
     * 忘记密码
     *
     * @param number
     * @param verificationCode
     */
    @Override
    public void forgetPwd(String number, String newPassword,SimpleCallBack callBack) {
        RetrofitFactory.getBean(ApiUrl.class).changePassword(number,newPassword)
                .enqueue(new Callback<Result<Boolean>>() {
                    @Override
                    public void onResponse(Call<Result<Boolean>> call, Response<Result<Boolean>> response) {
                        Result<Boolean> result;
                        if (response.isSuccessful() && (result = response.body()) != null){
                            boolean changeSucceed = result.getData();
                            if (changeSucceed){
                                callBack.onSuccess(null);
                            }else {
                                callBack.onError("修改失败！");
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<Result<Boolean>> call, Throwable t) {
                        callBack.onError(t.getMessage());
                    }
                });
    }
}
